package com.minutes.in.seconds.contacts;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import android.content.ContentProviderOperation;
import android.content.Context;
import android.content.OperationApplicationException;
import android.graphics.Bitmap;
import android.os.RemoteException;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.Photo;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import android.widget.Toast;

public class ContactsProvider {

	public static void addPersonToContacts(Context context, long id,
			String name, String lastName, String mobilePhone, String email, Bitmap bitmap) {
		ArrayList<ContentProviderOperation> ops =
		          new ArrayList<ContentProviderOperation>();

		int rawContactID = ops.size();
		 
        ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
                .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
                .withValue(RawContacts.ACCOUNT_NAME, null)
                .build());

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        if(bitmap!=null){ 
            bitmap.compress(Bitmap.CompressFormat.PNG , 75, stream);
            
            ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
        	        .withValueBackReference(Data.RAW_CONTACT_ID, rawContactID)
        	        .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
        	        .withValue(StructuredName.GIVEN_NAME, name)
        	        .withValue(StructuredName.FAMILY_NAME, lastName)
        	        .build());

        	 ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
        	        .withValueBackReference(Data.RAW_CONTACT_ID, rawContactID)
        	        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
        	        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, mobilePhone)
        	        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,  Phone.TYPE_MOBILE)
        	        .build());
        	 
        	 ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
        	         .withValueBackReference(Data.RAW_CONTACT_ID, rawContactID)
        	       .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
        	         .withValue(ContactsContract.CommonDataKinds.Email.DATA, email)
        	         .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK)
        	         .build());
        	 
        	 ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
        	         .withValueBackReference(Data.RAW_CONTACT_ID, rawContactID)
        	       .withValue(ContactsContract.Data.MIMETYPE, Photo.CONTENT_ITEM_TYPE)
        	         .withValue(ContactsContract.CommonDataKinds.Email.DATA, email)
        	         .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO,stream.toByteArray())
        	         .build());
            try {
                stream.flush();
            }catch (IOException e) {
                e.printStackTrace();
            }
        } else {
        	ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
        	        .withValueBackReference(Data.RAW_CONTACT_ID, rawContactID)
        	        .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
        	        .withValue(StructuredName.GIVEN_NAME, name)
        	        .withValue(StructuredName.FAMILY_NAME, lastName)
        	        .build());

        	 ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
        	        .withValueBackReference(Data.RAW_CONTACT_ID, rawContactID)
        	        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
        	        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, mobilePhone)
        	        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,  Phone.TYPE_MOBILE)
        	        .build());
        	 
        	 ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
        	         .withValueBackReference(Data.RAW_CONTACT_ID, rawContactID)
        	       .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
        	         .withValue(ContactsContract.CommonDataKinds.Email.DATA, email)
        	         .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK)
        	         .build());
        }
        
		try {
			context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
		} catch (RemoteException e) {
			Toast.makeText(context, "Unable to add contact", Toast.LENGTH_SHORT).show();
			e.printStackTrace();
		} catch (OperationApplicationException e) {
			Toast.makeText(context, "Unable to add contact", Toast.LENGTH_SHORT).show();
			e.printStackTrace();
		}
	}
}
